<template>
  <div>
    <van-nav-bar title="账号注册" left-arrow @click-left="$router.back()" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
    <router-link to="/login">已有账号？去登录</router-link>
  </div>
</template>

<script>
import { register } from '@/api/user'
export default {
  data() {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit(values) {
      const res = await register(values)
      this.$toast.fail(res.description)
      if (res.status === 200) this.$router.push('/login')
    },
  }
}
</script>

<style scoped>
a {
  display: block;
  margin: 0 auto;
  text-align: center;
  text-decoration: none;
  font-size: 0.175rem;
  color: #666;
}
</style>
